﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CustomsSystem.Model;
using CustomsSystem.BLL;
using System.Data;
using Maticsoft.Common;

namespace WEB.workflow
{
    public partial class workflowView : System.Web.UI.Page
    {
        public static DataSet ds;
        public bool isRecive;
        protected void Page_Load(object sender, EventArgs e)
        {
            WorkflowHandleManage bll = new WorkflowHandleManage();
            ds = new DataSet();

            if (!IsPostBack)
            {

                if (Session["JobNo"] == null) return;
                string strJobNo = Session["JobNo"].ToString();
                string strEmpNo = Session["EmpNo"].ToString();
                
                if (strJobNo == "00")
                    BindEmpData(ddlEmp);
                else
                    BindEmpData(ddlEmp, strJobNo);
            }
            string sqlQuery;
            string selectValue = ddlEmp.SelectedValue;
            if (0 == ddlWorkList.SelectedIndex)  //未完成的任务  jobEmpNo
            {
                //sqlQuery = string.Format("(jobNO='{0}' and receiveMark=0) or (jobEmpNo='{1}' and doEmpNo is null)", strJobNo, strEmpNo);
                sqlQuery = string.Format("jobNO ='{0}' and doEmpNo is null", selectValue);
                //if (ddlTimeLimit.SelectedValue != "0") sqlQuery += " and ontimeType = '" + ddlTimeLimit.SelectedValue + "'";
            }
            else               //已完成任务
            {
                
                sqlQuery = string.Format("jobNO ='{0}' and receiveMark=1", selectValue );
                //if (ddlTimeLimit.SelectedValue != "0") sqlQuery += " and ontimeType = '" + ddlTimeLimit.SelectedValue + "'";
            }
            ds = bll.GetList(sqlQuery);
            gvMyWorkflow.DataSource = ds;
            gvMyWorkflow.DataBind();

        }


        protected void gvMyWorkflow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMyWorkflow.PageIndex = e.NewPageIndex;
            gvMyWorkflow.DataBind();
        }
        protected void gvMyWorkflow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //判断是否是本人的任务
            if (e.CommandName == "go")//换页
            {
                try
                {
                    TextBox tb = (TextBox)gvMyWorkflow.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    gvMyWorkflow_PageIndexChanging(null, ea);
                }
                catch
                {

                }
            }
            if (e.CommandArgument == "Next" || e.CommandArgument == "First" || e.CommandArgument == "Prev" || e.CommandArgument == "Last")
            { }
            else
            {
                string index = e.CommandArgument.ToString(); //那行被选中,取出选中行的索引 
                string id = gvMyWorkflow.Rows[Convert.ToInt32(index)].Cells[0].Text;
                string sheetID = gvMyWorkflow.Rows[Convert.ToInt32(index)].Cells[1].Text;
                string theJobNo = ds.Tables[0].Rows[Convert.ToInt32(index)]["jobNO"].ToString();
                string curJobNo = Session["JobNo"].ToString();

                WorkflowHandleManage bll = new WorkflowHandleManage();
                CustomsSystem.Model.workflow model = bll.GetModel(int.Parse(id));
                if (e.CommandName == "Receive")
                {
                    if (theJobNo != curJobNo)
                    {
                        Response.Redirect("~/workflow/workflowAppointo.aspx?ID=" + sheetID);
                        return;
                    }
                    if (model.receiveMark == 1)
                    {
                        MessageBox.Show(this, "请不要重复接收，尽快完成工作后处理该单");
                        return;
                    }
                    model.receiveMark = 1;
                    string strEmpNo = Session["EmpNo"].ToString();
                    model.jobEmpNo = strEmpNo; //当前登陆员工编号
                    model.recDate = DateTime.Now;
                    model.doEmpNo = null;
                    if (bll.Update(model))
                    {
                        isRecive = true;
                        MessageBox.ShowAndRedirect(this, "接收成功！请完成工作后处理该单", "workflowView.aspx");
                    }
                }
                else if (e.CommandName == "Handle")
                {
                    if (theJobNo != curJobNo)
                    {
                        Response.Redirect("~/workflow/workflowAppointo.aspx?ID=" + sheetID);
                        return;
                    }
                    //先判断是否已经接收
                    if (model.receiveMark != 1)
                    {
                        MessageBox.Show(this, "该步骤尚未接收，请先接收");
                        return;
                    }
                    Response.Redirect(string.Format("workflowHandle.aspx?ID={0}&SHEETID={1}", id, sheetID));
                }
            }
        }
        
        

        protected void gvMyWorkflow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string empNo;
            string jobNo;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                WorkflowHandleManage bll = new WorkflowHandleManage();
                CustomsSystem.Model.workflow model;
                string id = e.Row.Cells[0].Text;
                int sheetID = Convert.ToInt32(e.Row.Cells[1].Text);
                model = bll.GetModel(int.Parse(id));
                //已完成，不判断时间
                if (model.doEmpNo!= null && model.doEmpNo != "")
                {
                    
                    //如果是历史记录，则隐藏两个按钮
                    Button btnRec = (Button)e.Row.Cells[8].Controls[0];
                    if (btnRec != null)
                    {
                        if (ddlWorkList.SelectedIndex == 1) { btnRec.Visible = false; }
                    }
                    Button btnHandle = (Button)e.Row.Cells[9].Controls[0];
                    if (btnHandle != null)
                    {
                        if (ddlWorkList.SelectedIndex == 1) { btnHandle.Visible = false; }
                    }
                    Image imgRec = (Image)e.Row.Cells[7].FindControl("ImgStatus");
                    if (imgRec != null)
                    {
                        string imagepath = "../images/status/1.png";
                        imgRec.ImageUrl = imagepath;//显示图片
                    }
                    Image tmp_ImageIntime = (Image)e.Row.Cells[6].FindControl("ImgIntime");//获得图片列的控件
                    if (tmp_ImageIntime != null)
                    {
                        string imagepath = "";
                        if (model.ontimeType == 1)
                        {
                            imagepath = "../images/status/Green.png";
                        }
                        else if (model.ontimeType == 2)
                        {
                            imagepath = "../images/status/Yellow.png";
                        }
                        else if (model.ontimeType == 3)
                        {
                            imagepath = "../images/status/Red.png";
                        }
                        tmp_ImageIntime.ImageUrl = imagepath;//显示图片
                    }
                    empNo = e.Row.Cells[4].Text;
                    e.Row.Cells[4].Text = ChangeNo2Name(empNo);

                   jobNo = e.Row.Cells[2].Text;
                    e.Row.Cells[2].Text = ChangeNo2JobName(jobNo);
                    return;
                }
                //未完成
                //判断是否接受
                Image tmp_Image = (Image)e.Row.Cells[7].FindControl("ImgStatus");//获得图片列的控件
                if (tmp_Image != null)
                {
                    string imagepath = "";
                    if (model.receiveMark == 0)
                    {
                        imagepath = "../images/status/0.png";
                    }
                    else
                    {
                        imagepath = "../images/status/1.png";
                    }
                    tmp_Image.ImageUrl = imagepath;//显示图片
                }
                //先判断报关还是报检
                WorkflowManage wfMan = new WorkflowManage();
                int type = 0;
                if (model.infoWKID != null) type = model.infoWKID.Value;
                Info_workflowMaster modelMaster = wfMan.GetModelMaster(type);
                if (modelMaster != null)
                {
                    if (modelMaster.sheetTypeID == 1) type = 1;
                    else if (modelMaster.sheetTypeID == 2) type = 2;
                }

                //判断及时度
                int IntimeFlag = bll.GetIntime(sheetID, type);
                Image tmp_ImageIntime2 = (Image)e.Row.Cells[6].FindControl("ImgIntime");//获得图片列的控件
                if (tmp_ImageIntime2 != null)
                {
                    string imagepath = "";
                    if (IntimeFlag == 1)
                    {
                        imagepath = "../images/status/Green.png";
                    }
                    else if (IntimeFlag == 2)
                    {
                        imagepath = "../images/status/Yellow.png";
                    }
                    else
                    {
                        imagepath = "../images/status/Red.png";
                    }
                    tmp_ImageIntime2.ImageUrl = imagepath;//显示图片
                }
                //转换编号为名字
                empNo = e.Row.Cells[4].Text;
                e.Row.Cells[4].Text = ChangeNo2Name(empNo);

                jobNo = e.Row.Cells[2].Text;
                e.Row.Cells[2].Text = ChangeNo2JobName(jobNo);
            }
        }

        /// <summary>
        /// 将员工编号转换为姓名
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        protected string ChangeNo2Name(string no)
        {
            //CustomsSystem.Model.Users model = new Users();
            UserManage bll = new UserManage();
            DataSet ds = bll.GetMyList("EMPNum='" + no + "'");
            string name = "";
            if (ds.Tables[0].Rows.Count > 0)
                name = ds.Tables[0].Rows[0]["UserName"].ToString();
            return name;
        }

        /// <summary>
        /// 将岗位编号转换为岗位名
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        protected string ChangeNo2JobName(string no)
        {
            JobManage bll = new JobManage();
            DataSet ds = bll.GetMyList(" jobNo ='" + no + "'");
            string name = "";
            if (ds.Tables[0].Rows.Count > 0)
                name = ds.Tables[0].Rows[0]["jobName"].ToString();
            return name;
        }

    

        /// <summary>
        /// 绑定员工数据
        /// </summary>
        /// <param name="ddl"></param>
        private void BindEmpData(DropDownList ddl)
        {
            JobManage jobBll = new JobManage();
            List<Info_job> jobModels = jobBll.GetModelList("");
            foreach (Info_job aJob in jobModels)
            {

                ddl.Items.Add(new ListItem(aJob.jobName, aJob.jobNo));
            }
        }

        private void BindEmpData(DropDownList ddl, string jobNO)
        {
            JobManage jobBll = new JobManage();
            List<Info_job> jobModels = jobBll.GetModelList("");
            foreach (Info_job aJob in jobModels)
            {
                int len = jobNO.Length;
                if (aJob.jobNo.Length >= len && aJob.jobNo.Substring(0, len) == jobNO)
                    ddl.Items.Add(new ListItem(aJob.jobName, aJob.jobNo));
            }
        }
        ///// <summary>
        ///// 跳页
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        gvMyWorkflow.DataBind();
        //        TextBox tb = (TextBox)gvMyWorkflow.BottomPagerRow.FindControl("tbinPageNum");
        //        int num = Int32.Parse(tb.Text.Trim());

        //        GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
        //        gvMyWorkflow_PageIndexChanging(null, ea);

        //    }
        //    catch { MessageBox.Show(this, "请输入数字页码"); }

        //}



    }
}